'use strict';

angular.module('myApp.teachers_add', [])

.controller('teachers_addCtrl', ['$scope','$routeParams','$http','breadcrumbData',
    function($scope,$routeParams,$http,$location,$breadcrumbData){
        $('#class_new').hide();

        function load(){
            $http.get('v1/subject').success(function(data){
                $scope.classes = [];
                var data = data.result;
                for (var i = 0; i < data.length; i++) {
                    $scope.classes.push({'id':data[i].id,"name":data[i].name});
                    $scope.teacher = {'subject_id':data[i].id};
                };
                $scope.classes.push({'id':'00',"name":'添加新课程'});
            });
        };
        load();
        if ($routeParams.id) {
            $http.get('v1/teacher/'+$routeParams.id).success(function(data){
                $scope.teacher = data.result[0];
            })
        };
        $scope.update = function(){
                if ($scope.teacher.subject_id=="00") {
                   $('#class_new').show();
                }else{
                    $('#class_new').hide();
                };
            }
        $scope.submit2 = function(){
            $http.post('v1/subject',{name:$scope.classNew}).success(function(data){
                if (!data.error) {
                    alert("新增成功","success");
                    load();
                    $('#class_new').hide();
                }else{
                    alert(data.message);
                };
            })
        }
        //跳转到class列表
        $scope.submit = function(){
            if ($routeParams.id) {
                $http.put('v1/teacher/'+$routeParams.id,$scope.teacher).success(function(data){
                    if( !data.error ){
                        alert(data.message);
                        window.location.href = '#/teachers';
                    }else{
                        alert(data.message);
                    }
                })
            }else{
                $http.post('v1/teacher',$scope.teacher).success(function(data){
                if( !data.error ){
                    alert("新增成功","success");
                    window.location.href = '#/teachers';
                }else{
                    alert(data.message);
                }
            });
            };
        };
}]);
